﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WHC.Hotel.Entity;
using WHC.Hotel.BLL;
using WHC.OrderWater.Commons;

namespace WHC.Hotel.UI
{
    public partial class FrmEditTeaWaiter : BaseForm
    {
        public string ID = string.Empty;

        public FrmEditTeaWaiter()
        {
            InitializeComponent();
        }

        private void SetInfo(TeaWaiterInfo info)
        {
            info.Area = this.cmbRegion.Text;
            info.Grade = this.cmbGrade.Text;
            info.IDNumber = this.txtIDNumber.Text;
            info.Name = this.txtName.Text.Trim();
            info.Note = this.txtNote.Text;
            info.Sex = this.cmbSex.Text;
            info.Telephone = this.txtTelephone.Text;
            info.WaiterNo = this.txtWaiterNo.Text;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            SaveData(false);
        }

        private void SaveData(bool isCreateOrder)
        {
            #region 输入验证
            if (this.txtName.Text.Length == 0)
            {
                MessageUtil.ShowTips("服务生姓名不能为空");
                this.txtName.Focus();
                return;
            }
            #endregion

            if (!string.IsNullOrEmpty(ID))
            {
                TeaWaiterInfo info = BLLFactory<TeaWaiter>.Instance.FindByID(ID);
                if (info != null)
                {
                    SetInfo(info);

                    try
                    {
                        bool succeed = BLLFactory<TeaWaiter>.Instance.Update(info, info.ID.ToString());
                        if (succeed)
                        {
                            MessageUtil.ShowTips("保存成功");
                            this.DialogResult = DialogResult.OK;

                            this.Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error(ex);
                        MessageUtil.ShowError(ex.Message);
                    }
                }
            }
            else
            {
                #region 新建保存
                TeaWaiterInfo info = new TeaWaiterInfo();
                SetInfo(info);

                try
                {
                    bool succeed = BLLFactory<TeaWaiter>.Instance.Insert(info);
                    if (succeed)
                    {
                        MessageUtil.ShowTips("保存成功");
                        this.DialogResult = DialogResult.OK;

                        this.Close();
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                    MessageUtil.ShowError(ex.Message);
                } 
                #endregion
            }
        }

        private void InitDictItem()
        {
            this.cmbGrade.Items.Clear();
            this.cmbGrade.Items.AddRange(DictItemUtil.GetWaiterGrade());

            this.cmbRegion.Items.Clear();
            this.cmbRegion.Items.AddRange(DictItemUtil.GetTeaServiceRegion());
        }

        private void FrmEditCustomer_Load(object sender, EventArgs e)
        {
            InitDictItem();
            if (!string.IsNullOrEmpty(ID))
            {
                this.Text = "编辑 " + this.Text;
                TeaWaiterInfo info = BLLFactory<TeaWaiter>.Instance.FindByID(ID);
                if (info != null)
                {
                    #region 显示客户信息
                    txtName.Text = info.Name;
                    txtNote.Text = info.Note;
                    txtIDNumber.Text = info.IDNumber;
                    txtTelephone.Text = info.Telephone;
                    txtWaiterNo.Text = info.WaiterNo;
                    cmbGrade.Text = info.Grade;
                    cmbRegion.Text = info.Area;
                    cmbSex.Text = info.Sex;

                    #endregion
                }

                //this.btnOK.Enabled = Portal.gc.HasFunction("Customer/Modify");
            }
            else
            {
                this.Text = "新建 " + this.Text;
                
                //this.btnOK.Enabled = Portal.gc.HasFunction("Customer/Add");
            }

            //this.btnNewOrder.Enabled = Portal.gc.HasFunction("Order/Add");
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
